/*
 ============================================================================
 Name		: RoImageDecoder.cpp
 Author	  : Leo
 Version	 : 1.0
 Copyright   : Your copyright notice
 Description : CRoImageDecoder implementation
 ============================================================================
 */

#include "RoImageDecoder.h"

CRoImageDecoder::CRoImageDecoder(MRoImageDecoderObserver& aObserver) :
	CActive(EPriorityStandard),iObserver(aObserver) // Standard priority
	{
	iBitmap = NULL;
	iMask = NULL;
	imageDecoder = NULL;
	}

CRoImageDecoder* CRoImageDecoder::NewLC(MRoImageDecoderObserver& aObserver)
	{
	CRoImageDecoder* self = new (ELeave) CRoImageDecoder(aObserver);
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
	}

CRoImageDecoder* CRoImageDecoder::NewL(MRoImageDecoderObserver& aObserver)
	{
	CRoImageDecoder* self = CRoImageDecoder::NewLC(aObserver);
	CleanupStack::Pop(); // self;
	return self;
	}

void CRoImageDecoder::ConstructL()
	{
	iRfs.Connect();
	CActiveScheduler::Add(this); // Add to scheduler
	}

CRoImageDecoder::~CRoImageDecoder()

	{
//	Cancel(); // Cancel any request, if outstanding
	iRfs.Close();
	
	// Delete instance variables if any
	}

void CRoImageDecoder::DoCancel()
	{
	this->Cancel();
	}
_LIT8(MimeType, "image/png");
void CRoImageDecoder::Decoder(const TDesC& aPath)
	{
	Cancel(); // Cancel any request, just to be sure
	delete imageDecoder;
	imageDecoder = NULL;
	iBitmap = new (ELeave) CFbsBitmap;
	iMask = new (ELeave) CFbsBitmap;
	imageDecoder = CImageDecoder::FileNewL(iRfs, aPath, MimeType, CImageDecoder::EOptionAlwaysThread);
	iBitmap->Create(imageDecoder->FrameInfo().iOverallSizeInPixels, imageDecoder->FrameInfo().iFrameDisplayMode /*was EColor4K*/);
	iMask->Create(imageDecoder->FrameInfo().iOverallSizeInPixels, EGray256 /*was EGray2*/ );
	imageDecoder->Convert( &iStatus, *iBitmap, *iMask );
	this->SetActive();
	}

void CRoImageDecoder::RunL()
	{
		if(! iObserver.Callback(iBitmap, iMask))
			{
				Cancel();
				delete imageDecoder;
				imageDecoder = NULL;
			}
		
		
	}

TInt CRoImageDecoder::RunError(TInt aError)
	{
	return aError;
	}



void LoadPng(RFs& aFs,const TDesC& aPath, CFbsBitmap* aBitmap, CFbsBitmap* aMask)
	{	
	CImageDecoder* imageDecoder = CImageDecoder::FileNewL(aFs,
	aPath, MimeType, CImageDecoder::EOptionAlwaysThread);
	aBitmap->Create(imageDecoder->FrameInfo().iOverallSizeInPixels, 
	imageDecoder->FrameInfo().iFrameDisplayMode /*was EColor4K*/);
	aMask->Create(imageDecoder->FrameInfo().iOverallSizeInPixels, EGray256 /*was EGray2*/ );
	TRequestStatus status;
	imageDecoder->Convert( &status, *aBitmap, *aMask );
	User::WaitForRequest( status );
	delete imageDecoder;
	};
